package edu.ynu.e_platform.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import edu.ynu.e_platform.common.entity.LogicEntity;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author manwen Yang
 */
@Entity
@Getter
@Setter
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
@Where(clause = "isDeleted = 0")  //逻辑删除,过滤掉删除标记不为0的
public class Shop extends LogicEntity {

    @Column
    private String shopName;

    @Column
    private String shopImg;

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"shops","items"})
    private Business business;

    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"shop"})
    private Set<ShopItem> shopItems;

    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"shop"})
    private Set<Order> orders;

    /**
     *  根据商品查找shopItem
     * @param item
     * @return shopItem
     */
    @JsonIgnore
    private ShopItem findShopItem(Item item){
        //并发加锁
        AtomicReference<ShopItem> result = new AtomicReference<>(null);
        shopItems.forEach(shopItem -> {
            if(shopItem.getItem().getId().equals(item.getId())){
                result.set(shopItem);
            }
        });

        return result.get();
    }

    /**
     *  新建商店商品，新建商品默认不上架
     * @param item
     * @return 返回创建好的商品
     */
    @JsonIgnore
    public ShopItem addItem(Item item){

        //商家只能添加属于自己的商品
        if(!item.getBusiness().getId().equals(business.getId())) {
            System.out.println("商品无效！！");
            return null;
        }

        ShopItem shopItem = new ShopItem();

        if (findShopItem(item)==null) {
            shopItem.setShop(this);
            shopItem.setItem(item);
            shopItem.setItemStatus(0);
            shopItems.add(shopItem);

        }

        return shopItem;
    }


    /**
     *  上架商品，可以上架已经新建了(商品在shopItems中)，也可直接上架新的商品，新的商品会默认创建
     * @param item
     * @return shopitem
     */
    @JsonIgnore
    public ShopItem putAwayItem(Item item){

        addItem(item);
        var shopitem = findShopItem(item);
        shopitem.setItemStatus(1);

        return shopitem;
    }

    /**
     *  下架商品
     * @param item
     * @return shopitem
     */
    @JsonIgnore
    public ShopItem removeItem(Item item){

        //在shopItems中改变该商品的状态
        for (ShopItem shopItem : shopItems) {
            if (shopItem.getId().equals(item.getId())) {
                shopItem.setItemStatus(0);

                return shopItem;
            }
        }
        return null;
    }

}
